'use strict';

const xtpl = require('xtpl');
const path = require('path');
const md5 = require('md5');
const captchapng = require('captchapng');
const db = require(path.join(__dirname,'../config/config'));
const CommonHelper = require(path.join(__dirname,"../helpers/commonHelpers"));
const fs = require('fs');


module.exports.weeklyList = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);
    //当前页码值
    const page = parseInt(req.query.page || 1);

    //每页显示多少条
    const limit = 12;

    //偏移量
    const start = (page-1)*limit;

    let where = 1;

    db.table('weekly').where(where).count('*').then(function(count){

        //总页数
        const totalPage = count % limit == 0 ? count/limit : parseInt(count/limit)+1;

        const totalPageArray = [];

        //分页
        for(var i=1;i<=totalPage;i++)
        {
            totalPageArray.push(i);
        }

        //查询数据
        db.table('weekly').alias('weekly').join({
            table: 'student',
            join: 'left',
            as: 'student',
            on: ['stu_id', 'stu_id']
        }).where(where).order('weekly.weekly_id DESC').limit(start,limit).select().then(function(weekly){

            let data = {
                array:weekly,
                count:count,
                totalPageArray:totalPageArray,
                page:page,
                totalPage:totalPage,
            };
            CommonHelper.renderFile(req,res,"weeklyList.html",data);
        });


    });

};


module.exports.weeklyAdd = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    db.table('student').select().then(function(result){
        let Data = {
            "student":result
        };

        CommonHelper.renderFile(req,res,"weeklyAdd.html",Data);        
    });
};

module.exports.weeklyAddForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    //先获取表单的数据    
       
    let data = {
        "weekly_name":req.body.weekly_name,
        "weekly_content":req.body.weekly_content,
        "weekly_time":Date.now(),
        "weekly_attr":req.body.weekly_attr,
        "stu_id":req.body.stu_id
    };  

    //是否有上传文件
    // if(req.files.weekly_attr.size > 0)
    // {
    //     data.weekly_attr = req.files.weekly_attr.path.replace('assets','');
    // }
    db.table('weekly').add(data).then(function(insertId){
        if(insertId){  
            //重定向
            res.redirect(`/weekly/weeklyList`);
            return false;
        }else{
            //重定向
            res.redirect(`/weekly/weeklyAdd`);
            return false;
        }
    });

};

module.exports.weeklyEdit = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let weekly_id = req.query.weekly_id;

    if(!weekly_id)
    {
        //重定向
        res.redirect(`/weekly/weeklyList`);
        return false;
    }

    db.table('weekly').where(`weekly_id = ${weekly_id}`).find().then(function(weekly){

        let Data = {
            "weekly":weekly
        };

        CommonHelper.renderFile(req,res,"weeklyEdit.html",Data);

    });
};

module.exports.weeklyEditForm = function(req,res)
{
    //判断是否登录
    CommonHelper.isLogin(req,res);

    let weekly_id = req.query.weekly_id;

    if(!weekly_id)
    {
        //重定向
        res.redirect(`/weekly/weeklyList`);
        return false;
    }

    db.table('weekly').where(`weekly_id = ${weekly_id}`).find().then(function(weekly){

        let data = {
            "weekly_name":req.body.weekly_name,
            "weekly_content":req.body.weekly_content,
            "weekly_attr":req.body.weekly_attr
        };

        //是否有上传文件
        // if(req.files.weekly_attr.size > 0){

        //     //同步获取文件的状态
        //     let weekly_old = fs.statSync(path.join("assets",weekly.weekly_attr));

        //     weekly_old.isFile() && fs.unlinkSync(path.join("assets",weekly.weekly_attr));

        //     data.weekly_attr = req.files.weekly_attr.path.replace('assets','');
        // }

        db.table('weekly').where(`weekly_id = ${weekly_id}`).update(data).then(function(affectId){
            if(affectId)
            {
                //重定向
                res.redirect(`/weekly/weeklyList`);
                return false;
            }else{
                //重定向
                res.redirect(`/weekly/weeklyEdit?weekly_id=${weekly_id}`);
                return false;
            }
        });
    });
};

module.exports.weeklyDelete = function(req,res){

    let weekly_id = req.query.weekly_id;

    if(!weekly_id)
    {
        //重定向
        res.redirect(`/weekly/weeklyList`);
        return false;
    }
    
    db.table('weekly').where(`weekly_id = ${weekly_id}`).delete().then(function(affectId){
        if(affectId){
            console.log('删除成功');
            //重定向
            res.redirect(`/weekly/weeklyList`);
            return false;
        }else{
            console.log('删除失败');
            //重定向
            res.redirect(`/weekly/weeklyList`);
            return false;
        }
    });
};